
import { createI18n } from 'vue-i18n'
import { App } from 'vue'
import { language } from './language'


function isValidKey(
  key: string | number | symbol,
  object: object
): key is keyof typeof object {
  return key in object;
}


let languageLocal = {
  zh: {},
  en: {},
  ind: {}
}

for (const key in language) {
  if (isValidKey(key, language)) {
    for (const subKey in language[key] as any) {
      if (isValidKey(subKey, language[key])) {
        languageLocal[subKey][key] = language[key][subKey]
      }
    }
  }
}

const i18n = createI18n({
  legacy: false,  // 没有该参数可能会报错
  locale: localStorage.getItem('lang') || 'zh',
  globalInjection: true,
  messages: {
    ...languageLocal
  }
})

export function useI18n(app: App) {
  app.use(i18n)
}
